//
//  HNSNProductListVCTableViewController.m
//  ZYManagementSystem
//
//  Created by 星道 on 17/6/14.
//  Copyright © 2017年 焕宁. All rights reserved.
//

#import "HNSNProductListVCTableViewController.h"
#import "HNSNProductListCell.h"
#import "HNProductListBuyHistoryVC.h"

@interface HNSNProductListVCTableViewController ()

@property (nonatomic, strong) NSMutableArray <HNSNProductListModel *>*DataArr;

//页数
@property (nonatomic, assign) NSInteger Index;
@end

@implementation HNSNProductListVCTableViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //设置页数默认为1
    self.Index = 1;
    //取消滚动条
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    //添加占位View
    self.tableView.tableFooterView = [UIView new];
    //设置预估高度
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 60;
    //取消灰线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //预注册
    [self.tableView registerNib:[UINib nibWithNibName:[HNSNProductListCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    //下拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self RefreshData];
    }];
    //开始加载
    [self.tableView.mj_footer beginRefreshing];
}





#pragma mark - 网络请求

/**
 网络请求
 */
- (void)RefreshData {

    NSDictionary *Params = @{
                             @"action":@"productList",
                             @"pageNumber":@(self.Index)
                           };
    [NetManager Get:LLBaseURL(@"yxhUser.do") Auth:YES Params:Params Success:^(id Obj) {


        //结束刷新
        [self EndRefresh];
        if ([Obj[@"code"] isEqualToNumber:@1000]) {

            //主数据存放的地方
            NSArray *Arr = (NSArray *)Obj[@"t"][@"dataList"];

            //为0零表示没有数据了
            if (Arr.count == 0) {
                dispatch_async(dispatch_get_main_queue(), ^{

                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                });
                return ;
            }
            //取值保存
            [Arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

                //保存
                HNSNProductListModel *Model = [HNSNProductListModel HN_ObjectWithKeyValus:obj];
                [self.DataArr addObject:Model];
            }];
            //页数加1
            self.Index += 1;
            //通知主线程刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });

        } else {

            [HUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *error) {
        //结束刷新
        [self EndRefresh];
        [HUD showError:NetErr];
    }];
}




/**
 结束刷新
 */
- (void)EndRefresh{

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
    });
}





#pragma mark - TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNSNProductListCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.Model = self.DataArr[indexPath.row];
    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNSNProductListModel *Model = self.DataArr[indexPath.row];
    HNProductListBuyHistoryVC *VC = [[HNProductListBuyHistoryVC alloc] init];
    VC.ID = Model.ID;
    [self.navigationController pushViewController:VC animated:YES];
}



#pragma mark - setter
- (NSMutableArray<HNSNProductListModel *> *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}
@end
